package com.logicwin.aibala.ui.screens.Search;

import com.logicwin.aibala.action.LoadSearchAction;
import com.logicwin.aibala.utils.Graphics;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Rect;
import android.graphics.Typeface;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;

public class SearchView extends View {
	
	private final String TAG = SearchView.class.getCanonicalName();
	private boolean mSetupReady = false;
	class SearchViewItem{
		public int Size = 0;
		public int PosX = 0;		
		public int PosY = 0;
		public int Width = 0;
		public int Height = 0;
		public int Color = 0;
		public String Word = "";
	}
	
	private int mViewWidth = 0;
	private int mViewHeight = 0;
	private int mLineHeight = 0;
	private final int mLines = 8;
	private final int mWordsNum = 13;
	private final int mBigWordSize = 48;
	private final int mSmallWordSize = 36;
	
	private final int mColors[] = new int[] {0xFF7FFFD4,0xFFFFB6C1,
			0xFFDC143C,0xFFFFF0F5,0xFFFF1493,
			0xFFC71585,0xFFDA70D6,0xFFD8BFD8,
			0xFFDDA0DD,0xFFDB7093,0xFFADD8E6,0xFF8A2BE2,
			0xFF7B68EE,0xFF0000CD,0xFF000080,0xFF00008B,
			0xFF00FA9A,0xFF006400,0xFF008080,0xFF008B8B,
			0xFF00FF00,0xFFFFF0F5,0xFFF0FFFF};
	/*{R.color.aquamarine,R.color.lightpink,
			R.color.crimson,R.color.lavenderblush,R.color.deeppink,
			R.color.mediumviolenred,R.color.orchid,R.color.thistle,
			R.color.plum,R.color.violet,R.color.lightblue,R.color.blueviolet,
			R.color.slateblue,R.color.mediumblue,R.color.navy,R.color.darkblue,
			R.color.springgreen,R.color.darkgreen,R.color.teal,R.color.cyan,
			R.color.lime,R.color.lavenderblush,R.color.azure};*/
	SearchViewItem[] mWords;
	
	public SearchView(Context context) {
		super(context);
		// TODO Auto-generated constructor stub
//		SetupData(context);
	}
	public SearchView(Context context, AttributeSet attrs) {
		super(context, attrs);
//		SetupData(context);
	}	
	public void SetupPosition(){
		Log.i("SetupData","width "+this.getWidth());
		Log.i("SetupData","height "+this.getHeight());		
		Rect rectSrc = new Rect();
		this.getLocalVisibleRect(rectSrc);
		Log.i("SetupData","LocalVisibleRect " + rectSrc);
		
		mViewWidth = rectSrc.width();
		mViewHeight = rectSrc.height();
		mLineHeight = mViewHeight/mLines;		
		
		this.getGlobalVisibleRect(rectSrc);
		Log.i("SetupData","GlobalVisibleRect " + rectSrc);
		Log.i("SetupData","left "+rectSrc.left + ",right" + rectSrc.right + ",top"+rectSrc.top
				+ ",bottom" + rectSrc.bottom);			

		mWords = new SearchViewItem[mWordsNum];
		for(int i=0; i<mWordsNum; i++)
			mWords[i] = new SearchViewItem();
		mWords[0].PosX = mViewWidth/5 << 1;
		mWords[0].PosY = mLineHeight;
		mWords[0].Size = mBigWordSize;
		
		mWords[1].PosX = mViewWidth>>3;
		mWords[2].PosY = mWords[1].PosY = mLineHeight<<1;
		mWords[2].Size = mWords[1].Size = mBigWordSize;		
		mWords[2].PosX = (mViewWidth>>1) + (mViewWidth/10);
		
		mWords[3].PosX = mViewWidth/3;		
		mWords[4].PosY = mWords[3].PosY = (mLineHeight<<1) + mLineHeight;
		mWords[4].Size = mWords[3].Size = mSmallWordSize;		
		mWords[4].PosX = mViewWidth/3 << 1;
		
		mWords[5].PosX = mViewWidth/20;
		mWords[7].PosY = mWords[6].PosY = mWords[5].PosY = mLineHeight<<2;
		mWords[5].Size = mWords[7].Size = mBigWordSize;
		mWords[6].Size = mSmallWordSize;
		mWords[6].PosX = (mViewWidth/5 << 1) ;		
		mWords[7].PosX = (mViewWidth/5 << 2) - mViewWidth/10;
		
		mWords[8].PosX = mViewWidth/3;
		mWords[9].PosY = mWords[8].PosY = (mLineHeight<<2) + mLineHeight;
		mWords[8].Size = mWords[9].Size = mSmallWordSize;
		mWords[9].PosX = (mViewWidth/3)<<1;
		
		mWords[10].PosX = mViewWidth>>3;
		mWords[11].PosY = mWords[10].PosY = (mLineHeight<<2) + (mLineHeight<<1);
		mWords[11].Size = mWords[10].Size = mBigWordSize;
		mWords[11].PosX = (mViewWidth/10) + (mViewWidth>>1);	
		
		mWords[12].PosX = mViewWidth/5 << 1;
		mWords[12].PosY = (mLineHeight<<3) - mLineHeight;
		mWords[12].Size = mBigWordSize;
	}
	
	public void SetupData(){
		String[] keywords = null;
		try{
			keywords = LoadSearchAction.getSearchKeywords();
		}
		catch(Exception e){
			Log.i(TAG, e.getMessage());
		}
		Log.i(TAG, "keyword size is" + keywords.length + "keywords are " +keywords);
		
		for(int i=0; i<mWords.length && i<keywords.length; i++){
			mWords[i].Word = keywords[i];
			mWords[i].Height = mWords[i].Size;
			int idx =(int) (Math.random()*mColors.length);
			mWords[i].Color = mColors[idx] ;
			Paint p = new Paint();
			Typeface font = Typeface.create("宋体",Typeface.BOLD);
			p.setTypeface(font);
			p.setTextSize(mWords[i].Size);
			mWords[i].Width  = (int)p.measureText(mWords[i].Word);			
		}
		
	}
	public String HitScreen(float xPos, float yPos){

		Log.i("SearchView::SetupData","ViewWidth "+mViewWidth + ",ViewHeight "+mViewHeight);	
//		yPos = yPos-mStartYPos;
		Log.i("HitScreen","xPos "+xPos+",yPos "+ yPos);
		
		for( int i=0; i<mWords.length; i++){
			if(	xPos >= mWords[i].PosX && 
				xPos <= mWords[i].PosX + mWords[i].Width &&
				yPos >= mWords[i].PosY &&
				yPos <= mWords[i].PosY + mWords[i].Height)
				return mWords[i].Word;
		}
		return null;
	}

	protected void onDraw(Canvas canvas) {
		
		if(!mSetupReady){
			SetupPosition();
			SetupData();
			mSetupReady = true;
		}
		
		try {
			for(int i = 0; i<mWords.length; i++)
			{
				Graphics.drawText(canvas, mWords[i].Word, mWords[i].PosX, 
						mWords[i].PosY+mWords[i].Size , mWords[i].Size, mWords[i].Color);				
			}			
		} catch (Exception e) {
			Log.e("ScreenSearch::onDraw", e.toString());
		}		
	}	
}
